package com.example.administrator.food.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.food.R;
import com.example.administrator.food.modle.club.fragment.ClubFragment;
import com.example.administrator.food.modle.home.fragment.HomeFragment;
import com.example.administrator.food.modle.me.fragment.MeFragment;
import com.example.administrator.food.modle.shop.fragment.ShopFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private FragmentManager manager;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.radiogroup_main);
        manager = getSupportFragmentManager();
        ((RadioButton) radioGroup.getChildAt(0)).setChecked(true);
        addFragment();

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int childCount = group.getChildCount();
                int index = 0;
                for (int i = 0; i < childCount; i++) {
                    RadioButton childAt = (RadioButton) radioGroup.getChildAt(i);
                    if (childAt.isChecked()) {
                        index = i;
                        if(index>=childCount/2)
                        {
                            index--;
                        }
                        showFragment(index);
                        break;
                    }
                }

            }
        });
    }

    private void showFragment(int index) {
        FragmentTransaction transaction = manager.beginTransaction();
        for (int i = 0; i < fragments.size(); i++) {
            if (index == i) {
                transaction.show(fragments.get(index));
            } else {
                transaction.hide(fragments.get(i));
            }
        }
        transaction.commit();
    }

    private void addFragment() {
        fragments.add(HomeFragment.newInstance());
        fragments.add(ClubFragment.newInstance());
        fragments.add(ShopFragment.newInstance());
        fragments.add(MeFragment.newInstance());

        FragmentTransaction transaction = manager.beginTransaction();
        for (int i = 0; i < fragments.size(); i++) {
            if(!fragments.get(i).isAdded())
                transaction.add(R.id.container_main,fragments.get(i)).show(fragments.get(i));
            transaction.hide(fragments.get(i));
        }
        transaction.show(fragments.get(0));
        transaction.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
